package main;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Component;
import javax.swing.Box;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.FlowLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JToggleButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.aptech.mwg.etown.pojo.Company;
import com.aptech.mwg.etown.server.business.CompanyManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.List;

public class managercompany extends JPanel {
	private JTable table;
	private JTextField txtCom;
	private JTextField txtbusiness;
	private JPanel panel_1;
	private JPanel panel_3;
	private JScrollPane scrollPane ;
	private JTextField txtDes;
	/**
	 * Create the panel.
	 */
	public managercompany() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 32, 36, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		Component verticalGlue = Box.createVerticalGlue();
		GridBagConstraints gbc_verticalGlue = new GridBagConstraints();
		gbc_verticalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_verticalGlue.gridx = 1;
		gbc_verticalGlue.gridy = 0;
		add(verticalGlue, gbc_verticalGlue);
		
		Component horizontalGlue = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
		gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalGlue.gridx = 0;
		gbc_horizontalGlue.gridy = 1;
		add(horizontalGlue, gbc_horizontalGlue);
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);
		scrollPane.setPreferredSize(new Dimension(550, 150));
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Check","CompanyID","Comapny Name", "Business", "Description", "Create Date"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(75);
		table.getColumnModel().getColumn(1).setPreferredWidth(115);
		table.getColumnModel().getColumn(2).setPreferredWidth(115);
		table.getColumnModel().getColumn(3).setPreferredWidth(115);
		table.getColumnModel().getColumn(4).setPreferredWidth(116);
		table.getColumnModel().getColumn(5).setPreferredWidth(116);
		JCheckBox check = new JCheckBox();
		table.getColumn("Check").setCellEditor(new DefaultCellEditor(check));
		scrollPane.setViewportView(table);
		
		Component horizontalGlue_1 = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue_1 = new GridBagConstraints();
		gbc_horizontalGlue_1.insets = new Insets(0, 0, 5, 0);
		gbc_horizontalGlue_1.gridx = 2;
		gbc_horizontalGlue_1.gridy = 2;
		add(horizontalGlue_1, gbc_horizontalGlue_1);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 3;
		add(panel, gbc_panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnNewButton = new JButton("Search");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    panel_1.setVisible(true);
			    panel_3.setVisible(true);
			}
		});
		panel.add(btnNewButton);
		
		JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    incom in = new incom();
                            in.setVisible(true);		
			}
		});
		panel.add(btnInsert);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(btnUpdate);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(btnDelete);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    setVisible(false);
			}
		});
		panel.add(btnCancel);
		
		panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 5);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 4;
		add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel_1.setVisible(false);
		JLabel label = new JLabel("Company: ");
		panel_1.add(label);
		
		txtCom = new JTextField();
		txtCom.setColumns(10);
		panel_1.add(txtCom);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		panel_1.add(horizontalStrut);
		
		JLabel lblBusiness = new JLabel("Business: ");
		panel_1.add(lblBusiness);
		
		txtbusiness = new JTextField();
		txtbusiness.setColumns(10);
		panel_1.add(txtbusiness);
		
		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		panel_1.add(horizontalStrut_3);
		
		JLabel lblDescription = new JLabel("Description: ");
		panel_1.add(lblDescription);
		
		txtDes = new JTextField();
		panel_1.add(txtDes);
		txtDes.setColumns(10);
		
		panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(0, 0, 0, 5);
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 5;
		add(panel_3, gbc_panel_3);
		panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel_3.setVisible(false);
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    	CompanyManager commanager = new CompanyManager();
				DefaultTableModel model = (DefaultTableModel)table.getModel();
				while(model.getRowCount() > 0)
				{
				    model.removeRow(0);
				}
				List<Company> list = commanager.Search_condition(txtCom.getText(),txtbusiness.getText(), txtDes.getText());
				for(Company com: list){
				    int CompanyID = com.getCompanyId();
				    String ComName = com.getName();
				    String Email = com.getBusiness();
				    String Phone = com.getDescription();
				    Date Createdate = com.getCreate_date();
				    Object[] newRow = new Object[] {new Boolean(false), CompanyID,ComName, Email, Phone, Createdate};
				    model.addRow(newRow);
				    System.out.println("load success" );
				}
			}
		});
		panel_3.add(btnOk);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_1);
		
		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    txtbusiness.setText("");
			    txtCom.setText("");
			    txtDes.setText("");
			}
		});
		panel_3.add(btnReset);
		
		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_2);
		
		JButton btnCancel_1 = new JButton("Cancel");
		btnCancel_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    panel_1.setVisible(false);
			    panel_3.setVisible(false);
			    loadtable();
			    
			}
		});
		panel_3.add(btnCancel_1);

	}
	
	public void loadtable(){
	    DefaultTableModel model = (DefaultTableModel)table.getModel();
		model.setRowCount(0);
		CompanyManager commanager = new CompanyManager();
		List<Company> listCom = commanager.list();
		for (Company com : listCom) {
		    	int CompanyID = com.getCompanyId();
			String ComName = com.getName();
			String Email = com.getBusiness();
			String Phone = com.getDescription();
			Date Createdate = com.getCreate_date();
			Object[] newRow = new Object[] {new Boolean(false), CompanyID,ComName, Email, Phone, Createdate};
			model.addRow(newRow);
			System.out.println("load success" );
		}
		table.repaint();
	}
}
